package com.dp.appi.service.impl;

import com.dp.appi.dao.ExplorationDao;
import com.dp.appi.dao.ProjectStatisticsDao;
import com.dp.appi.dao.SysConfigDao;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ProjectStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/23.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProjectStatisticsServiceImpl implements ProjectStatisticsService {
    @Autowired
    private ProjectStatisticsDao projectStatisticsDao;

    @Autowired
    private SysConfigDao sysConfigDao;

    @Autowired
    private ExplorationDao explorationDao;

    /**
     * 个人项目进度统计
     * openIdent
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> scheduleCount(String username) {
        Map<String, Object> userMap = sysConfigDao.getUserByOpenIdent(username);
        return projectStatisticsDao.scheduleCount(userMap.get("id"), userMap.get("org_id"));
    }

    /**
     * 阶段线路杆塔明细
     *
     * @param voltageGrade
     * @param stageId
     * @return
     */
    @Override
    public List<Map<String, Object>> poleSum(String voltageGrade, Long stageId, String id) {
        int classify = explorationDao.getClassify(stageId, id);
        return projectStatisticsDao.poleSum(voltageGrade, stageId, classify);
    }

    /**
     * 阶段台区材料统计
     *
     * @param explor_id
     * @return
     */
    @Override
    public List<Map<String, Object>> materialSum(Long stage_id, Integer classify, Long explor_id, String exploration_type) {
        return projectStatisticsDao.materialSum(stage_id, classify, explor_id, exploration_type);
    }

    /**
     * 阶段用电户
     *
     * @param stage_id
     * @return
     */
    @Override
    public List<Map<String, Object>> userPoint(Long stage_id, String id) {
        int classify = explorationDao.getClassify(stage_id, id);
        return projectStatisticsDao.userPoint(stage_id, classify);
    }

    /**
     * 项目设计阶段统计
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> projectSum(Map<String, String> param, String id, Object org_id) {
        List<Map<String, Object>> res = projectStatisticsDao.projectSum(param, id, null, org_id);
        Map<String, Object> m = new HashMap<>();
        int total = 0;
        m.put("review", 0);
        m.put("preDesign", 0);
        m.put("consDesign", 0);
        m.put("completed", 0);
        for (int i = 0; null != res && i < res.size(); i++) {
            Map<String, Object> mr = res.get(i);
            Object stageObj = mr.get("stage");
            Object num = mr.get("num");
            if (null == stageObj || num == null) continue;

            if ("11".equals(String.valueOf(stageObj))) {
                m.put("review", num);
            } else if ("12".equals(String.valueOf(stageObj))) {
                m.put("preDesign", num);
            } else if ("13".equals(String.valueOf(stageObj))) {
                m.put("consDesign", num);
            } else if ("14".equals(String.valueOf(stageObj))) {
                m.put("completed", num);
            }
            total += Integer.valueOf(String.valueOf(num));
        }
        m.put("total", total);
        return m;
    }

    /**
     * 项目按字段统计
     *
     * @param param
     * @param id    (项目类型 1、项目属性 2、项目进度 3、资金来源 4 )
     */
    @Override
    public List<Map<String, Object>> fieldSum(Map<String, String> param, String id, Object org_id) {
        String groupBy = "";
        String statisticsType = param.get("statisticsType");
        if (null == statisticsType || "".equals(statisticsType)) {
            throw new CommonException("EXPO013");
        }
        if ("1".equals(statisticsType)) {
            groupBy = "b.project_type";
        } else if ("2".equals(statisticsType)) {
            groupBy = "b.project_attribute";
        } else if ("3".equals(statisticsType)) {
            groupBy = "a.current_schedule";
        } else if ("4".equals(statisticsType)) {
            groupBy = "b.funds_sources";
        }
        List<Map<String, Object>> res = projectStatisticsDao.projectSum(param, id, groupBy, org_id);
        return res;
    }

    /**
     * 交叉跨越描述
     *
     * @param explorId
     * @return
     */
    @Override
    public String describe(Long stage_id, Integer classify, Long explorId, String exploration_type) {
        return projectStatisticsDao.describe(stage_id, classify, explorId, exploration_type);
    }

    /**
     * 交叉跨越列表
     *
     * @param stage_id
     * @return
     */
    @Override
    public List<Map<String, Object>> corssList(Long stage_id, Integer classify, Long explor_id, String exploration_type) {
        return projectStatisticsDao.corssList(stage_id, classify, explor_id, exploration_type);
    }

    /**
     * 阶段交叉跨越
     *
     * @param stage_id
     * @return
     */
    @Override
    public Map<String, ?> pointSum(Long stage_id, String id, Long explor_id) {
        int classify = explorationDao.getClassify(stage_id, id);
        Map<String, Object> ps = new HashMap<>();
        if (null == explor_id) {
            List<Map<String, Object>> pl = projectStatisticsDao.pointSum(stage_id, classify);
            ps.put("trans_num", 0);
            ps.put("pole_num", 0);
            ps.put("user_num", 0);
            for (Map<String, Object> map : pl) {
                if (null == map.get("pt_id")) continue;
                if ("100002".equals(String.valueOf(map.get("pt_id")))) {//柱上变压器
                    ps.put("trans_num", map.get("num"));
                }
                if ("100004".equals(String.valueOf(map.get("pt_id")))) {//杆塔
                    ps.put("pole_num", map.get("num"));
                }
                if ("100005".equals(String.valueOf(map.get("pt_id")))) {//下户点
                    ps.put("user_num", map.get("num"));
                }
            }
        }
        ps.put("route", projectStatisticsDao.routeListByV(stage_id, classify, explor_id));
        return ps;
    }

    /**
     * 项目阶段台区统计
     *
     * @param explor_id
     * @return
     */
    @Override
    public Map<String, ?> explorSum(Long explor_id) {
        Map<String, Object> ps = new HashMap<>();
        Map<String, Object> pl = projectStatisticsDao.explorSum(explor_id);
        ps.put("trans_capacity", pl.get("trans_capacity"));
        ps.put("pole_num", pl.get("num"));

//        List<Map<String, Object>> explorList = new ArrayList<>();
//        Map<String, Object> explorMap = new HashMap<>();
//        explorMap.put("exploration_id",explor_id);
//        explorList.add(explorMap);
//        ps.put("route",projectStatisticsDao.routeList(explorList));
        ps.put("route", projectStatisticsDao.routeList1(explor_id));
        return ps;
    }
}
